package com.VolunteerPlatform.Service.TeamService.Impl;

import com.VolunteerPlatform.Dao.TeamInfoDao;
import com.VolunteerPlatform.Dao.VolunteerInfoDao;
import com.VolunteerPlatform.Entity.TeamInfo;
import com.VolunteerPlatform.Entity.VolunteerInfo;
import com.VolunteerPlatform.Service.TeamService.TeamInfoService;
import com.VolunteerPlatform.Service.UtilityService.CheckFormatUtility;
import com.VolunteerPlatform.Service.UtilityService.ExportDataExcel;
import com.VolunteerPlatform.Service.UtilityService.ReadExcelService;
import com.VolunteerPlatform.Service.UtilityService.WriteExcelService;
import com.sun.xml.internal.ws.policy.privateutil.PolicyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by DoveXL on 2018/12/9.
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class TeamInfoServiceImpl implements TeamInfoService {
    @Autowired
    private TeamInfoDao teamInfoDao;

    @Autowired
    private VolunteerInfoDao volunteerInfoDao;

    @Autowired
    private ReadExcelService readExcelService;
    @Autowired
    private WriteExcelService writeExcelService;


    @Autowired
    private CheckFormatUtility checkFormatUtility;


    public List<TeamInfo> getAllTeamInfoService(String TeamName, int Page, int PageList)
    {
        return teamInfoDao.getAllTeamInfo(TeamName,Page,PageList);
    }

    public int insertTeamInfoService(String TeamName,int TeamNumber,String TeamLeading,String LeadingPhonenum)
    {
        return teamInfoDao.insertTeamInfo(TeamName,TeamNumber,TeamLeading,LeadingPhonenum);
    }

    public int updateTeamInfoService(String OriginTeamName,String TeamName,int TeamNumber,String TeamLeading,String LeadingPhonenum)
    {
        int teamnumber = teamInfoDao.updateTeamInfo(OriginTeamName,TeamName,TeamNumber,TeamLeading,LeadingPhonenum);
        List<VolunteerInfo> volunteerInfos = volunteerInfoDao.getVolunteerInfo(null,null,null,OriginTeamName,null,-1,-1,-1);
        int VolunteerNumber = volunteerInfos.size();
        List<Map<String,String>> volunteer = new ArrayList<>();

        if(teamnumber==1 && VolunteerNumber==0)
        {
            return 1;
        }else if(teamnumber==1&&VolunteerNumber>0) {
            if(OriginTeamName.equals(TeamName))
            {
                return 1;
            }else {

                for (int i = 0; i < VolunteerNumber; i++) {
                    Map<String, String> map = new HashMap<>();
                    map.put("IDCardNumber", volunteerInfos.get(i).getIDCardNumber());
                    map.put("Unit", TeamName);
                    volunteer.add(map);
                }
                int Number = volunteerInfoDao.updateVolunteerInfoBatch(volunteer, 1);
                if (Number == VolunteerNumber) {
                    return 1;
                } else {
                    return 0;
                }
            }
        }else
        {
            return 0;
        }
    }
    public int deleteTeamInfoService(String TeamName)
    {
        List<VolunteerInfo> volunteerInfos = volunteerInfoDao.getVolunteerInfo(null,null,null,TeamName,null,0,-1,-1);
        int VolunteerNumber = volunteerInfos.size();
        List<Map<String,String>> volunteer = new ArrayList<>();
        int teamnumber = teamInfoDao.deleteTeamInfo(TeamName);
        if(teamnumber==1 && VolunteerNumber==0)
        {
            return 1;
        }else if(teamnumber==1&&VolunteerNumber>0) {
                for (int i = 0; i < VolunteerNumber; i++) {
                    Map<String, String> map = new HashMap<>();
                    map.put("IDCardNumber", volunteerInfos.get(i).getIDCardNumber());
                    map.put("Deleted", "1");
                    volunteer.add(map);
                }
                int Number = volunteerInfoDao.updateVolunteerInfoBatch(volunteer, 0);
                if (Number == VolunteerNumber) {
                    return 1;
                } else {
                    return 0;
                }
        }else
        {
            return 0;
        }
    }


    public int ImportBatchTeamInfoService(String filename) throws IOException
    {
         List<TeamInfo> teamInfoList = teamInfoDao.getAllTeamInfo(null,-1,-1);
         List<String> teamname = new ArrayList<>();
         for(int i=0;i<teamInfoList.size();i++)
         {
             teamname.add(teamInfoList.get(i).getTeamName().trim());
         }
         List<TeamInfo> teamInfoListImport = readExcelService.getExcelInfo(filename);
         List<Integer> RemoveList = new ArrayList<>();
         if(teamInfoListImport.size()==0)
         {
             return 0;
         }
         List<Map<String,String>> ErrorMap = new ArrayList<Map<String,String>>();


         for(int i=0;i<teamInfoListImport.size();i++)
         {
             String TeamName =teamInfoListImport.get(i).getTeamName().trim();
             String Teamleading = teamInfoListImport.get(i).getTeamLeading();
             String LeadingPhonenum = teamInfoListImport.get(i).getLeadingPhonenum();
             Map<String,String> map = new HashMap<>();
             if (teamname.contains(String.valueOf(TeamName)))
             {
                 map.put("TeamName",TeamName);
                 map.put("TeamLeading",Teamleading);
                 map.put("LeadingNumber",LeadingPhonenum);
                 map.put("ErrorMsg","该单位名称在系统中已经存在");
                 ErrorMap.add(map);
                 System.out.println("1"+TeamName);
                 RemoveList.add(i);
             }else{
                 System.out.println("2"+TeamName);
                 if(!TeamName.equals(""))
                 {
                     if(!checkFormatUtility.isChinese(TeamName))
                     {
                         map.put("TeamName",TeamName);
                         map.put("TeamLeading",Teamleading);
                         map.put("LeadingNumber",LeadingPhonenum);
                         map.put("ErrorMsg","该单位名称不全是中文");
                         ErrorMap.add(map);
                         RemoveList.add(i);;
                     }else{
                         if (!Teamleading.equals("无")) {
                             if (!checkFormatUtility.isChinese(Teamleading)) {
                                 map.put("TeamName", TeamName);
                                 map.put("TeamLeading", Teamleading);
                                 map.put("LeadingNumber", LeadingPhonenum);
                                 map.put("ErrorMsg", "该负责人不全是中文");
                                 ErrorMap.add(map);
                                 RemoveList.add(i);;
                             } else {
                                 if (!LeadingPhonenum.equals("无")) {
                                     if (!checkFormatUtility.isMobile(LeadingPhonenum)) {
                                         map.put("TeamName", TeamName);
                                         map.put("TeamLeading", Teamleading);
                                         map.put("LeadingNumber", LeadingPhonenum);
                                         map.put("ErrorMsg", "该联系方式格式不正确");
                                         ErrorMap.add(map);
                                         RemoveList.add(i);
                                     }
                                 }
                             }
                         }
                     }
                 }else {
                     map.put("TeamName",TeamName);
                     map.put("TeamLeading",Teamleading);
                     map.put("LeadingNumber",LeadingPhonenum);
                     map.put("ErrorMsg","该单位名称为空");
                     ErrorMap.add(map);
                     RemoveList.add(i);
                 }
             }
         }
        String[] title={"单位名称","负责人","负责人联系方式","错误信息"};
        String[] FieldName={"TeamName","TeamLeading","LeadingNumber","ErrorMsg"};
         List<TeamInfo> Listd = new ArrayList<>();
         for (int j=0;j<teamInfoListImport.size();j++)
         {
             if(!RemoveList.contains(j))
             {
                 Listd.add(teamInfoListImport.get(j));
             }
         }

         if(Listd.size()==0)
         {
             writeExcelService.CreateTeamInfoExcel(ErrorMap,filename,title,FieldName);
             return 5;
         }


         int number = teamInfoDao.batchInsertTeamInfo(Listd);
         if(number==teamInfoListImport.size() && ErrorMap.size()==0)
         {
             return 1;
         }else if(number==teamInfoListImport.size() && ErrorMap.size()>0)
         {
             writeExcelService.CreateTeamInfoExcel(ErrorMap,filename,title,FieldName);
             return 2;
         }else if(number!=teamInfoListImport.size())
         {
             return 3;
         }else {
             return 4;
         }
    }


    public int ExportTeamInfoService(String title,String[] rowName,List<String> checkFieldNameList,String filename) throws IOException
    {
        List<TeamInfo> teamInfoList = teamInfoDao.getAllTeamInfo(null,-1,-1);
        List<Object []> StringList = new ArrayList<>();
        int TeamInfoSize = teamInfoList.size();
        for(int i=0;i<TeamInfoSize;i++)
        {
            Map<String,String> TeamInfoMap = new HashMap<>();
            TeamInfo teamInfo = teamInfoList.get(i);
            TeamInfoMap.put("TeamName",teamInfo.getTeamName());
            TeamInfoMap.put("TeamNumber",String.valueOf(teamInfo.getTeamNumber()));
            TeamInfoMap.put("TeamLeading",teamInfo.getTeamLeading());
            TeamInfoMap.put("LeadingPhonenum",teamInfo.getLeadingPhonenum());
            String[] dataArray = new String[checkFieldNameList.size()];
            List<String> dataList = new ArrayList<>();
            for(int j = 0;j<checkFieldNameList.size();j++)
            {
                dataList.add(TeamInfoMap.get(checkFieldNameList.get(j)));
            }
            dataList.toArray(dataArray);
            StringList.add(dataArray);
        }
        ExportDataExcel exportDataExcel = new ExportDataExcel(title,rowName,StringList,filename);
        try {
            exportDataExcel.ExportExcel();
            return 1;
        }catch (Exception e)
        {
            e.printStackTrace();
            throw  new IOException();
        }
    }







}
